<?php


namespace App\Exceptions;


use App\Services\ResponseCode;
use Illuminate\Contracts\Validation\Validator;

/**
 * 自定义验证失败异常
 * Class CustomValidateException
 * @package App\Exceptions
 */
class CustomValidateException extends \Exception
{
    protected Validator $validator;

    public function __construct(Validator $validator)
    {
        $this->validator = $validator;
    }

    public function render()
    {
        $messages = $this->validator->errors()->messages();

        $string = '';

        foreach ($messages as $field => $msg) {
            $string .= $field . ':' . implode(',', $msg);
        }

        throw new BusinessException($string, ResponseCode::FORM_VALIDATE_FAIL);
    }
}
